package com.example.demo_one.app.service.impl;

import com.example.demo_one.app.service.RedisService;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {

    private RedisTemplate redisTemplate;

    @Override
    public void deleteSingleKey(Object key) {
        redisTemplate.delete(key);
    }

    @Override
    public void deleteMultipleKey(List<Object> keys) {
        keys.stream().forEach(o -> redisTemplate.delete(o));
    }

    @Override
    public void expire(Object key, Long time, TimeUnit timeUnit) {
        redisTemplate.expire(key, time, timeUnit);
    }

    @Override
    public Long getExpire(Object key) {
        return redisTemplate.getExpire(key);
    }

    @Override
    public boolean isHasKey(Object key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public void setStringKeyBoundValueOperations(Object key, Object value) {
        redisTemplate.boundValueOps(key).set(value);
    }

    @Override
    public void setStringKeyValueOperations(HashMap<Object, Object> hashMapKeyAndValue) {
        hashMapKeyAndValue.entrySet().forEach(entry -> redisTemplate.opsForValue().set(entry.getKey(), entry.getValue()));
    }

    @Override
    public String getStringKeyBoundValueOperations(Object key) {
        return redisTemplate.boundValueOps(key).getKey().toString();
    }

    @Override
    public List<Object> getStringKeyValueOperations(List<Object> keys) {
        List<Object> values = new ArrayList<>();
        keys.stream().forEach(string -> values.add(redisTemplate.opsForValue().get(string).toString()));
        return values;
    }

    @Override
    public void setHashKeyBoundValueOperations(Object key, HashMap<Object, Object> valueMap) {

        valueMap.entrySet().forEach(entry -> redisTemplate.boundHashOps(key).put(entry.getKey(), entry.getValue()));
    }

    @Override
    public void setHashKeyBoundValueOperationsHashMap(HashMap<Object, HashMap<Object, Object>> hashMapKeyAndValue) {
        hashMapKeyAndValue.entrySet().forEach(entry -> redisTemplate.boundHashOps(entry.getKey()).putAll(entry.getValue()));
    }

    @Override
    public void setHashKeyHashOperations(Object key, HashMap<Object, Object> valueMap) {
        valueMap.entrySet().forEach(entry -> redisTemplate.opsForHash().put(key, entry.getKey(), entry.getValue()));
    }

    @Override
    public Map getHashKeyBoundValueOperations(Object key) {
        return redisTemplate.boundHashOps(key).entries();
    }

    @Override
    public List getHashKeyList(Object key) {
        return redisTemplate.boundHashOps(key).values();
    }

    @Override
    public String getHashKeyString(Object key, Object valueKey) {
        return redisTemplate.opsForHash().get(key, valueKey).toString();
    }

    @Override
    public void deleteHashMapValue(Object key, List<Object> valueKeys) {
        valueKeys.stream().forEach(o -> redisTemplate.boundHashOps(key).delete(o));
    }

    @Override
    public boolean isHashMapOfValueKey(Object key, Object valueKey) {
        return redisTemplate.boundHashOps(key).hasKey(valueKey);
    }

    @Override
    public void setSetKey(Object key, List<Object> values) {
        redisTemplate.boundSetOps(key).add(values);
    }

    @Override
    public Set<Object> getSetKey(Object key) {
        return redisTemplate.boundSetOps(key).members();
    }

    @Override
    public boolean isSetValue(Object key, Object value) {
        return redisTemplate.boundSetOps(key).isMember(value);
    }

    @Override
    public void deleteSetValue(Object key, List<Object> values) {
        values.stream().forEach(o -> redisTemplate.boundSetOps(key).remove(o));
    }

    @Override
    public Long getSetSize(Object key) {
        return redisTemplate.boundSetOps(key).size();
    }

    @Override
    public void setListKey(String flag, Object key, List<Object> valueList) {
        if (flag.equals(LEFTLIST)){
            redisTemplate.boundListOps(key).leftPushAll(valueList);
        }else if (flag.equals(RIGHTLIST)){
            redisTemplate.boundListOps(key).rightPushAll(valueList);
        }
//        redisTemplate.boundListOps(key).leftPush(valueList.get(1));
//        redisTemplate.opsForList().leftPush(key,valueList.get(1));
    }

    @Override
    public List getListKey(Object key, Long start, Long end) {
//        return redisTemplate.boundListOps(key).range(start,end);
//        redisTemplate.opsForList().range("test", 0, -1));  // 获取缓存的List所有的值
        return redisTemplate.opsForList().range(key,start,end);
    }

    @Override
    public Object getListKeyIndex(Object key, Long index) {
        return redisTemplate.opsForList().index(key,index);
    }

    @Override
    public void updateListKey(Object key, Long index, Object value) {
        redisTemplate.opsForList().set(key,index,value);
//        redisTemplate.boundListOps(key).set(index,value);
    }

    @Override
    public void deleteListKey(Object key, Long index, Object value) {
        redisTemplate.opsForList().remove(key,index,value);
//        redisTemplate.boundListOps(key).remove(index,value);
    }
}
